package com.zbkj.front.controller;

import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.model.order.Order;
import com.zbkj.common.model.user.User;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.response.*;
import com.zbkj.common.result.CommonResult;
import com.zbkj.common.vo.LogisticsResultVo;
import com.zbkj.front.service.FrontOrderService;
import com.zbkj.service.service.MerchantService;
import com.zbkj.service.service.OrderService;
import com.zbkj.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * H5端订单操作
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/front/order")
@Api(tags = "订单")
public class OrderController {

    @Autowired
    private FrontOrderService orderService;
    @Autowired
    private OrderService merOrderService;
    @Autowired
    UserService userService;
    @Autowired
    MerchantService merchantService;

    @ApiOperation(value = "预下单")
    @RequestMapping(value = "/pre/order", method = RequestMethod.POST)
    public CommonResult<OrderNoResponse> preOrder(@RequestBody @Validated PreOrderRequest request) {
        return CommonResult.success(orderService.preOrder_V1_5(request));
    }

    @ApiOperation(value = "加载预下单")
    @RequestMapping(value = "load/pre/{preOrderNo}", method = RequestMethod.GET)
    public CommonResult<PreOrderResponse> loadPreOrder(@PathVariable String preOrderNo) {
        return CommonResult.success(orderService.loadPreOrder(preOrderNo));
    }

    @ApiOperation(value = "计算订单价格")
    @RequestMapping(value = "/computed/price", method = RequestMethod.POST)
    public CommonResult<ComputedOrderPriceResponse> computedPrice(@Validated @RequestBody OrderComputedPriceRequest request) {
        return CommonResult.success(orderService.computedOrderPrice(request));
    }

    @ApiOperation(value = "创建订单")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<OrderNoResponse> createOrder(@Validated @RequestBody CreateOrderRequest orderRequest) {

        return CommonResult.success(orderService.createOrder(orderRequest));
    }


    @ApiOperation(value = "订单列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<OrderFrontDataResponse>> orderList(@ModelAttribute @Validated OrderFrontListRequest request) {
        return CommonResult.success(CommonPage.restPage(orderService.list_v1_4(request)));
    }

    @ApiOperation(value = "商家订单列表")
    @RequestMapping(value = "/shop/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<OrderFrontDataResponse>> shopOrderList(@ModelAttribute @Validated OrderFrontListRequest request) {
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success();
        }
        request.setMerId(merchant.getId());
        return CommonResult.success(CommonPage.restPage(orderService.list_v1_4(request)));
    }

    @ApiOperation(value = "订单详情")
    @RequestMapping(value = "/detail/{orderNo}", method = RequestMethod.GET)
    public CommonResult<OrderFrontDetailResponse> orderDetail(@PathVariable String orderNo) {
        return CommonResult.success(orderService.frontDetail(orderNo));
    }

    @ApiOperation(value = "订单取消")
    @RequestMapping(value = "/cancel/{orderNo}", method = RequestMethod.POST)
    public CommonResult<Boolean> cancel(@PathVariable(value = "orderNo") String orderNo) {
        if (orderService.cancel(orderNo)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "订单商品评论列表")
    @RequestMapping(value = "/reply/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<InfoReplyResponse>> replyList(@ModelAttribute PageParamRequest pageRequest) {
        return CommonResult.success(CommonPage.restPage(orderService.replyList(pageRequest)));
    }

    @ApiOperation(value = "评价订单商品")
    @RequestMapping(value = "/reply/product", method = RequestMethod.POST)
    public CommonResult<Boolean> replyProduct(@RequestBody @Validated OrderProductReplyRequest request) {
        if (orderService.replyProduct(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "删除订单")
    @RequestMapping(value = "/delete/{orderNo}", method = RequestMethod.POST)
    public CommonResult<Boolean> delete(@PathVariable(value = "orderNo") String orderNo) {
        if (orderService.delete(orderNo)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "订单收货")
    @RequestMapping(value = "/take/delivery/{orderNo}", method = RequestMethod.POST)
    public CommonResult<String> take(@PathVariable(value = "orderNo") String orderNo) {
        if (orderService.takeDelivery(orderNo)) {
            return CommonResult.success("订单收货成功");
        }
        return CommonResult.failed("订单收货失败");
    }

    @ApiOperation(value = "获取订单发货单列表")
    @RequestMapping(value = "/{orderNo}/invoice/list", method = RequestMethod.GET)
    public CommonResult<OrderInvoiceFrontResponse> getInvoiceList(@PathVariable(value = "orderNo") String orderNo) {
        return CommonResult.success(orderService.getInvoiceList(orderNo));
    }

    @ApiOperation(value = "物流信息查询")
    @RequestMapping(value = "/logistics/{invoiceId}", method = RequestMethod.GET)
    public CommonResult<LogisticsResultVo> getLogisticsInfo(@PathVariable(value = "invoiceId") Integer invoiceId) {
        return CommonResult.success(orderService.getLogisticsInfo(invoiceId));
    }

    @ApiOperation(value = "获取订单状态图")
    @RequestMapping(value = "/status/image", method = RequestMethod.GET)
    public CommonResult<List<HashMap<String, Object>>> getOrderStatusImage() {
        return CommonResult.success(orderService.getOrderStatusImage());
    }

    @ApiOperation(value = "查询商户已完成订单总金额")
    @RequestMapping(value = "/getMoneyByMers", method = RequestMethod.POST)
    public CommonResult<BigDecimal> getMoneyByMer(MersMoneyRequest request) {
        BigDecimal allMoney = new BigDecimal(0);
        for (Integer metId : request.getMerIds()) {
            OrderSearchAdminRequest tmp = new OrderSearchAdminRequest();
            tmp.setMerId(metId);
            tmp.setStatus("complete");
            List<Order> result = merOrderService.queryMerIdAndStatus(tmp);
            if (!result.isEmpty()) {
                for (Order order : result) {
                    allMoney = allMoney.add(order.getPayPrice());
                }
            }
        }
        return CommonResult.success(allMoney);
    }


    /*
     * 获取某一天商户们的已完成订单
     * */
    @ApiOperation(value = "查询某一天商户们的已完成订单")
    @RequestMapping(value = "/getOrdersByMersDate", method = RequestMethod.POST)
    public CommonResult<List<Order>> getMoneyByMerDay(MersOrderDateRequest request) {
        List<Order> result = new ArrayList<>();
        for (Integer metId : request.getMerIds()) {
            List<Order> tmp = merOrderService.findPayByDate(metId, request.getDate());
            result.addAll(tmp);
        }
        return CommonResult.success(result);
    }

    @ApiOperation(value = "核销订单")
    @PostMapping(value = "/verifyOrderNo/{code}")
    public CommonResult<Boolean> verifyOrderCode(@PathVariable("code") String code) {
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success(false);
        }

        return CommonResult.success(merOrderService.verificationOrderByCode(code, merchant.getId()));
    }

    @ApiOperation(value = "核销员核销订单")
    @PostMapping(value = "/shop/user/verifyOrderNo/{code}")
    public CommonResult<Boolean> shopUserVerifyOrderCode(@PathVariable("code") String code) {
        User user = userService.getInfo();

        return CommonResult.success(merOrderService.shopUserVerifyOrderCode(user.getId(), code));
    }

    @ApiOperation(value = "查询商家统计")
    @PostMapping(value = "/getStatistics")
    public CommonResult<Map<String, Object>> getStatistics() {
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success(new HashMap<>());
        }
        return CommonResult.success(merOrderService.getStatistics(merchant.getId()));
    }
}
